package Q868

import kotlin.math.max

class Main {

}

fun main(){
    println(Solution().binaryGap(5))
}

/**
 * 二进制间距
 */
class Solution {
    fun binaryGap(n: Int): Int {
        val binStr = Integer.toBinaryString(n)
        var lastOneIdx = n
        var max = 0
        for (i in binStr.indices) {
            if (binStr[i] == '1') {
                max = max(max, i - lastOneIdx)
                lastOneIdx = i
            }
        }
        return max
    }
}


